package com.ruoyi.hatha.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.hatha.domain.PMyvip;
import com.ruoyi.hatha.service.IPMyvipService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hatha.domain.PCards;
import com.ruoyi.hatha.service.IPCardsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员卡Controller
 * 
 * @author Dong
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/hatha/cards")
public class PCardsController extends BaseController
{
    @Autowired
    private IPCardsService pCardsService;
    @Autowired
    private IPMyvipService myvipService;

    /**
     * 查询会员卡列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PCards pCards)
    {
        startPage();
        List<PCards> list = pCardsService.selectPCardsList(pCards);
        return getDataTable(list);
    }

    @GetMapping("/listall")
    public AjaxResult listall(PCards pCards)
    {
        List<PCards> list = pCardsService.selectPCardsList(pCards);
        for(int i=0;i<list.size();i++){
            Long uid = SecurityUtils.getUserId();
            PMyvip pm = new PMyvip();
            pm.setCardId(list.get(i).getId());
            pm.setUid(uid);
            List<PMyvip> lpm = myvipService.selectPMyvipList(pm);

            if(lpm.size()==0){
                list.get(i).setMyvip(null);
            }else{
                list.get(i).setMyvip(lpm.get(0));
            }
        }
        return AjaxResult.success(list);
    }


    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id)
    {
        Long uid = SecurityUtils.getUserId();
        PCards data = pCardsService.selectPCardsById(id);
        PMyvip pm = new PMyvip();
        pm.setCardId(data.getId());
        pm.setUid(uid);
        List<PMyvip> lpm = myvipService.selectPMyvipList(pm);

        if(lpm.size()==0){
            data.setMyvip(null);
        }else{
            data.setMyvip(lpm.get(0));
        }
        return AjaxResult.success(data);
    }

    /**
     * 导出会员卡列表
     */
    @PreAuthorize("@ss.hasPermi('hatha:cards:export')")
    @Log(title = "会员卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PCards pCards)
    {
        List<PCards> list = pCardsService.selectPCardsList(pCards);
        ExcelUtil<PCards> util = new ExcelUtil<PCards>(PCards.class);
        util.exportExcel(response, list, "会员卡数据");
    }

    /**
     * 获取会员卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('hatha:cards:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pCardsService.selectPCardsById(id));
    }

    /**
     * 新增会员卡
     */
    @PreAuthorize("@ss.hasPermi('hatha:cards:add')")
    @Log(title = "会员卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PCards pCards)
    {
        return toAjax(pCardsService.insertPCards(pCards));
    }

    /**
     * 修改会员卡
     */
    @PreAuthorize("@ss.hasPermi('hatha:cards:edit')")
    @Log(title = "会员卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PCards pCards)
    {
        return toAjax(pCardsService.updatePCards(pCards));
    }

    /**
     * 删除会员卡
     */
    @PreAuthorize("@ss.hasPermi('hatha:cards:remove')")
    @Log(title = "会员卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pCardsService.deletePCardsByIds(ids));
    }
}
